package com.cell.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cell.common.States;
import com.cell.common.exceptions.exceptions.UserException;
import com.cell.dao.user.UserDao;
import com.cell.dao.user.UserLoginAccountDao;
import com.cell.pojo.DTO.user.validation.validateCodeDTO;
import com.cell.pojo.entity.user.User;
import com.cell.pojo.entity.user.UserLoginAccount;
import com.cell.pojo.entity.user.UserValidation;
import com.cell.dao.user.UserValidationDao;
import com.cell.service.user.IUserValidationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cell.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@Service
public class UserValidationServiceImpl extends ServiceImpl<UserValidationDao, UserValidation> implements IUserValidationService {
    @Autowired
    UserDao userDao;
    @Autowired
    UserLoginAccountDao loginAccountDao;
    @Autowired
    UserValidationDao validationDao;
    @Autowired
    UserUtil util;

    @Override
    public User validateCode(validateCodeDTO validateCodeDTO) {
        QueryWrapper wrapper = new QueryWrapper();
        String stateOfLoginKey = "";
        String loginKey = validateCodeDTO.getLoginKey();
        //判断用户是否使用手机号登录
        if (util.isNumeric(loginKey)) {
            wrapper.eq("phone", validateCodeDTO.getLoginKey());
            stateOfLoginKey = "phone";
        } else {
            wrapper.eq("email", validateCodeDTO.getLoginKey());
            stateOfLoginKey = "email";
        }

        //获取登录账户id，通过该信息获取用户验证码账户id
        UserLoginAccount loginAccount = loginAccountDao.selectOne(wrapper);
        if (loginAccount == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "用户不存在");
        }

        Long loginAccountId = loginAccount.getId();
        wrapper.clear();
        wrapper.eq(loginAccountId != null, "login_account_id", loginAccountId);
        User user = userDao.selectOne(wrapper);

        //若用户注销或不存在
        if (user == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "用户不存在");
        }
        //获取验证码id后通过id查找账户并验证验证码
        UserValidation validation = validationDao.selectById(user.getValidationId());
        //手机和邮箱验证码不存储在同一位置
        String backValidCode = "phone".equals(stateOfLoginKey) ? validation.getPhoneCode() : validation.getEmailCode();

        if (!validateCodeDTO.getCode().equals(backValidCode)) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "验证码错误");
        }

        return user;
    }

    @Override
    public void sendEmailForLogin(String email) {
        if (email.isEmpty()) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "邮箱不能为空");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("email", email);
        UserLoginAccount loginAccount = loginAccountDao.selectOne(wrapper);
        if (loginAccount == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "该邮箱尚未绑定");
        }
        wrapper.clear();
        wrapper.eq("login_account_id", loginAccount.getId());
        User user = userDao.selectOne(wrapper);

        //获取发送的验证码
        String code = util.sendValidEMail(email);
        validationDao.updateById(validationDao.selectById(user.getValidationId()).setEmailCode(code));

    }

    @Override
    public void sendSmsForLogin(String phone) {
        if (phone.isEmpty()) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "手机号不能为空");
        }
        if (!util.isNumeric(phone)) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "手机号格式不正确");
        }


        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        UserLoginAccount loginAccount = loginAccountDao.selectOne(wrapper);
        if (loginAccount == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "该手机号尚未注册");
        }
        wrapper.clear();
        wrapper.eq("login_account_id", loginAccount.getId());
        User user = userDao.selectOne(wrapper);

        //获取发送的验证码
        String code = util.sendValidSms(phone);
        validationDao.updateById(validationDao.selectById(user.getValidationId()).setPhoneCode(code));
    }

    @Override
    public void sendEmail(int userId) {
        User user = userDao.selectById(userId);

        //通过用户id查找到用户并通过用户查找其登录账户里的邮箱信息
        String email = loginAccountDao.selectById(userDao.selectById(userId).getLoginAccountId()).getEmail();
        if (email == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "邮箱未绑定");
        }
        //获取发送的验证码
        String code = util.sendValidEMail(email);
        validationDao.updateById(validationDao.selectById(user.getValidationId()).setEmailCode(code));
    }

    @Override
    public void sendSms(int userId) {
        User user = userDao.selectById(userId);

        //通过用户id查找到用户并通过用户查找其登录账户里的邮箱信息
        String phone = loginAccountDao.selectById(user.getLoginAccountId()).getPhone();
        if (phone == null) {
            throw new UserException(States.ERR_USER_NOT_FOUND.getStates(), "用户不存在");
        }

        //获取发送的验证码
        String code = util.sendValidSms(phone);
        validationDao.updateById(validationDao.selectById(user.getValidationId()).setPhoneCode(code));
    }
}
